<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        X-admin v1.0
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../static/css/x-admin.css" media="all">
    <link rel="stylesheet" href="../../static/css/dj-css.css" media="all">
</head>

<body>
<div class="x-body" style="padding: 0;">
        <fieldset class="layui-elem-field layui-field-title" >
            <legend>新闻</legend>
        </fieldset>
    <div id="addNews" class="addNews" style="padding: 20px;">
    <form class="layui-form layui-form-pane" method="post" action="" >
        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                标题
            </label>
            <div class="layui-input-inline">
                <input type="text" id="title" name="title" v-model="title"  required="" lay-verify="required"
                       placeholder="请输入标题" autocomplete="off" class="layui-input" value="">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red" style="font-size: 14px;">*必填</span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="dj-select dj-select-inline boxStyle">
                <label   style="height:auto;padding:3px 15px;width:110px;">新闻类型</label>
                <div class="dj-select-content" style="width: 180px;">
                    <div class="dj-select-input" flag = 'true' style="width: 173px;">
                        <span class="dj-selected readOnly" type="text" name=""  selectedCode='' style="line-height:40px;height:40px;width: 173px;" id="newsType">请选择新闻类型</span>
                        <i></i>
                    </div>
                    <ul class="dj-select-list" style="width: 173px;">
                        <li code="">请选择新闻类型</li>
                        <li v-for="item in newsTypeList" :code="item.code" :class="{'selected':item.code == newsType}">{{item.name}}</li>
                    </ul>
                </div>
            </div>
            <div class="layui-form-mid layui-word-aux layui-input-inline" style="float:none;display: inline-block;">
                <span class="x-red" style="font-size: 14px;">*必填</span>
            </div>

        </div>

        <div class="layui-form-item">
            <label for="author" class="layui-form-label">
                作者
            </label>
            <div class="layui-input-inline">
                <input type="text" id="author" name="author" v-model="author"   required="" lay-verify="required"
                       placeholder="请输入作者" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red" style="font-size: 14px;">*必填</span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="site-demo-upload">
                <img id="LAY_demo_upload" >
                <input type="hidden" id="img" v-model="img" >
                <div class="site-demo-upbar">
                    <input type="file" name="file" class="layui-upload-file" id="fileUpload">
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <!--<label for="editor" class="layui-form-label">-->
                <!--&lt;!&ndash;内容&ndash;&gt;-->
            <!--</label>-->
            <div class="layui-input-inline">
                <script id="editor" name="content"  type="text/plain" style="width:500px;height:250px;"></script>
                <input type="hidden" v-model="editorContent" id="editorContent" @change="giveContent()">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="add">提交</button>
            </div>
        </div>
    </form>

    </div>
</div>
<script src="../../static/lib/jquery.min.js"></script>
<script src="../../static/lib/vue.min.js"></script>
<script src="../../static/lib/layui/layui.js" charset="utf-8"></script>
<script src="../../static/lib/x-layui.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8" src="../../static/lib/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../../static/lib/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="../../static/lib/ueditor/lang/zh-cn/zh-cn.js"></script>
<script src="../../static/lib/dj-js.js"></script>
<script>
    var ue = UE.getEditor('editor', {
        toolbars: [
            ['fullscreen', 'source', 'undo', 'redo', 'bold','simpleupload','insertimage']
        ]
   });

    var VM = new Vue({
        el:"#addNews",
        data:{
            id:'',
            title:'',
            type:'',
            author:'',
            newsType:'',
            content:'',
            img:'',
            newsLists:[],
            newsTypes:[],
            msg:'成功',
            editorContent:'',
            realpath:'',
            newsTypeList:[]
        },
        created:function(){
//            this.getNews();
            this.ue();
            this.getNewsTypeList();
        },
        watch:{
            editorContent: function(){
                this.giveContent();
            }
        },
        methods:{
            ue:function(){
                UE.getEditor('editor', {
                    toolbars: [
                        ['fullscreen', 'source', 'undo', 'redo', 'bold','simpleupload','insertimage']
                    ]
                });
            },
            giveContent:function(){
                var _this=this;
                alert(_this.editorContent);
                ue.setContent(_this.editorContent);
            },
            getNewsTypeList:function(){
                var _this = this;
                $.ajax({
                    url:"../../dictionaries/getDic.do?type=news",
                    dataType:"json",
                    type:"POST",
                    async:false,
                    success:function (res) {
                        _this.newsTypeList  = res;
                    }

                })
            },
        }
    });

    layui.use(['form','layer','upload'], function(){
        $ = layui.jquery;
        var form = layui.form()
                ,layer = layui.layer;
        layui.upload({
            url: '../../multipart-file/upload.do'
            ,elem: '#fileUpload' //指定原始元素，默认直接查找class="layui-upload-file"
            ,success: function(res){
                $("#img").val(res.result.url);
                LAY_demo_upload.src = res.result.realpath;
            }
        });
        //自定义验证规则
        form.verify({
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function(value){
                if($('#password').val()!=$('#password2').val()){
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            var newsType11 = $("#newsType").attr("selectedCode");
            $.ajax({
                url:"../../news/save.do",
                dataType:"json",
                data:{
                    title:$("#title").val(),
                    author:$("#author").val(),
                    newsType:newsType11,
                    content:ue.getContent() ,
                    img:$("#img").val(),
                },
                type:"POST",
                async:false,
                success:function (res) {
                    if(res.msg=="success"){
                        layer.alert("编辑成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    }else{
                        layer.msg("增加失败", {icon: 1});
                    }
                },
                error:function (XMLHttpRequest) {
                    layer.msg("系统错误", {icon: 1});
                }
            })

            return false;
        });


    });
</script>
</body>

</html>
